<?php
/**
 * main config
 * 
 * 
 * @category   Config
 * @package    Config
 * @author     Xinze <xinze@live.cn>
 * @copyright  Copyright (c) 2010, 黄新泽
 * @version    1.0
 * @todo       
 */
//定义系统路径
if(!defined('ROOT_PATH'))
{
    define('ROOT_PATH', substr(str_replace('\\', '/', dirname(__FILE__)), 0, -12));
    define('LIB_PATH', ROOT_PATH . '/libraries');   //ZeroPHP Framework 所在目录
    define('APP_PATH', ROOT_PATH . '/app');         //应用程序目录

    define('MOD_PATH', APP_PATH . '/models');

    $themes_name = 'default';

    /**
     * 风格静态文件文件目录，此处变量名称$themes勿修改
     * 
     * @var string
     */
    if (isset($_SERVER['DOCUMENT_ROOT']) && $_SERVER['DOCUMENT_ROOT'])
    {
        $pro_path_row = explode($_SERVER['DOCUMENT_ROOT'], ROOT_PATH);
        $themes = $pro_path_row[1] . '/app/static/' . $themes_name;
    }
    else
    {
        $themes = '/app/static/' . $themes_name;
    }

    define('TPL_PATH', APP_PATH . '/views/' . $themes_name);
    define('CTL_PATH', APP_PATH . '/controllers');
    define('INI_PATH', APP_PATH . '/configs');

    /*
    define('HLP_PATH', APP_PATH . '/helpers');
    define('LOG_PATH', APP_PATH . '/data/logs');
    define('LAN_PATH', APP_PATH . '/data/locales');
    */

    //是否开启debug，如果为true，则不生成runtime缓存
    define('DEBUG', true);

    /**
     * Friend Url 规则
     * 
     * @var string
     */
    $pattern  = ':ctl/:met/:typ/*';
    $rules    = array();
    $defaults = array('ctl'=>'Main_Index', 'met'=>'index', 'typ'=>'e');

    global $import_file_row;

    if (!isset($import_file_row))
    {
        $import_file_row = array();
    }

    //公用函数库
    require_once LIB_PATH . '/__init__.php';
}
else
{
}

//插件启动
$plugin_rows = array(
                    //array('name'=>'Demo'),
                    //array('name'=>'Xhprof'),
                    //array('name'=>'Refresh', 'cli'=>false)
                );

//if (1 == mt_rand(1, 10000))
if (true)
{
    array_push($plugin_rows, array('name'=>'Xhprof'));
}


//$plugin_rows   = get_active_plugins();
$PluginManager = Zero_Plugin_Manager::getInstance($plugin_rows);
$PluginManager->trigger('init', '');
//$PluginManager->trigger('perm', '');

//初始化语言包
if (function_exists('_'))
{
    //init_locale(APP_PATH . '/data/locales/', 'zh_CN', 'HelloWorld');   //初始化，只需要一次即可
    //textdomain('HelloWorld');
}

if ('cli' != SAPI)
{
    set_time_limit(5); //运行时间限制一定要有的。 切记！

    //是否压缩输出
    $gzipcompress = 0;

    if($gzipcompress && function_exists('ob_gzhandler'))
    {
        ob_start('ob_gzhandler');
    }
    else
    {
        $gzipcompress = 0;
        ob_start();
    }

    Zero_Registry::set('gzipcompress', $gzipcompress);

    //负载控制，理论上，不高于处理器数目*0.7，因为机器高峰负载，可以高一些。
    Zero_Registry::set('loadctrl', 4);

    if (Zero_Registry::isRegistered('loadctrl'))
    {
        if (!function_exists('sys_getloadavg'))
        {
            function sys_getloadavg()
            {
                //$loadavg_file = '/proc/loadavg';
                //if (file_exists($loadavg_file)) {
                    //return explode(chr(32),file_get_contents($loadavg_file));
                //}

                if ('WIN'!=substr(PHP_OS, 0, 3)) 
                {
                    if ($fp = @fopen('/proc/loadavg', 'r')) 
                    {
                        $loadaverage = explode(' ', fread($fp, 6));
                        fclose($fp);

                        return $loadaverage;
                    }
                }
                else
                {
                    return array(0,0,0);
                }
            }
        }

        $loadaverage = sys_getloadavg();

        if ($loadaverage[0] > Zero_Registry::get('loadctrl')) 
        {
            header("HTTP/1.0 503 Too busy, try again later");
            echo 'Server busy, please try again later!';
            exit();
        }
    }


    if (function_exists('session_cache_limiter'))
    {
        session_cache_limiter('private, must-revalidate');
    }

    header('Content-type: text/html; charset=UTF-8');

    //强制过期，ajax请求不需要要加随机字符串
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header ('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
    header ('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: no-cache'); // HTTP/1.0

    header('P3P: CP=CAO PSA OUR');  //ie iframe cookie
}

Zero_Registry::set('base_url', 'http://www.xinze.me/');
Zero_Registry::set('index_page', 'index.php');

if (true || DEBUG)
{
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
}
else
{
    ini_set('display_errors', 1);
    error_reporting(E_ERROR|E_WARNING|E_PARSE);
}

//记录错误提示
ini_set('log_errors', 1);
ini_set('error_log', APP_PATH . '/data/logs/debug.log');

//设置时区
if(function_exists('date_default_timezone_set'))
{
    date_default_timezone_set('Asia/Shanghai');
    //date_default_timezone_set('UTC');
}


/*
应用cache, 如果使用cache，配置格式必须严格按照如下格式
0|false:none
1|true:file/memcache
*/
define('CHE', 1); 

if (CHE)
{
    //cache配置参数
    //require_once INI_PATH . '/cache.ini.php';

    $config_cache['memcache'] = array(
        array(
            '192.168.1.2' => '11211'
        ) ,
    );

    //设置cache 参数
    //cacheType 1:file  2:memcache
    $config_cache['base'] = array(
        'cacheType' => 1,
        'cacheDir' => APP_PATH . '/data/cache/base_cache/',
        'memoryCaching' => true,
        'automaticSerialization' => true,
        'hashedDirectoryLevel'      => 1,
        'lifeTime' => 3600
    );

    $config_cache['html'] = array(
        'cacheDir' => APP_PATH . '/data/cache/html_cache/',
        'memoryCaching' => false,
        'automaticSerialization' => false,
        'hashedDirectoryLevel'      => 2,
        'lifeTime' => 3600
    );
    
    Zero_Registry::set('config_cache', $config_cache);

}

//包含Db配置文件，如果使用DB，配置格式必须严格按照如下格式
//require_once INI_PATH . '/db.ini.php';
define('DB_DRIVE', 'Zero_Db_Pdo');
define('DB_DEBUG', true);

$config['db_cfg_rows'] = array(
                        'master'=>    array(
                                        'main' => array(
                                                            array(
                                                                'host' => '192.168.1.14',
                                                                'port' => '3306',
                                                                'user' => 'root',
                                                                'password' => '111111',
                                                                'database' => 'test',
                                                                'charset' => 'UTF8'
                                                            )
                                                          ),
                                        'ext'      => array(
                                                            array(
                                                                'host' => '192.168.1.14',
                                                                'port' => '3306',
                                                                'user' => 'root',
                                                                'password' => '111111',
                                                                'database' => 'test_ext',
                                                                'charset' => 'UTF8'
                                                            )
                                                          )
                                        ),

                        'slave'=>    array(
                                        'main' => array(
                                                            array(
                                                                'host' => '192.168.1.14',
                                                                'port' => '3306',
                                                                'user' => 'root',
                                                                'password' => '111111',
                                                                'database' => 'test',
                                                                'charset' => 'UTF8'
                                                            )
                                                          ),
                                        'ext'      => array(
                                                            array(
                                                                'host' => '192.168.1.14',
                                                                'port' => '3306',
                                                                'user' => 'root',
                                                                'password' => '111111',
                                                                'database' => 'test_ext',
                                                                'charset' => 'UTF8'
                                                            )
                                                          )
                                        )
    );

//通过这儿设置默认Db， 目前从主库读取数据，示例如下
$config['db_write_read'] = 'master';

//如果需要从slave库中读取， 需要设置如下：$db_cfg_rows['default'] = $db_cfg_rows['slave'];
Zero_Registry::set('db_cfg', $config);

//初始化参数
//require_once(INI_PATH . '/init.php');
?>